package com.zzy.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.zzy.commonutils.R;
import com.zzy.servicebase.exceptionhandler.GuliException;
import com.zzy.vod.service.VodService;
import com.zzy.vod.utils.ConstantVodUtils;
import com.zzy.vod.utils.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Author KAZU
 * @Date 2023/1/16 12:05
 * @Version 1.0
 */
@SuppressWarnings({"all"})
@RestController
@RequestMapping("/eduvod/video")
@CrossOrigin
public class VodController {
	
	@Autowired
	private VodService vodService;

	// 上传视频到阿里云
	@PostMapping("uploadAlyVideo")
	public R uploadAlyVideo(MultipartFile file) throws IOException {
		String videoId = vodService.uploadVideoAly(file);
		return R.ok().data("videoId", videoId);
	}
	
	// 删除阿里云视频
	@DeleteMapping("removeAlyVideo/{id}")
	public R removeAlyVideo(@PathVariable String id) {
		try {
			// 初始化对象
			DefaultAcsClient client = InitVodClient.initVodClient(
					ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
			// 创建删除视频request对象
			DeleteVideoRequest request = new DeleteVideoRequest();
			// 向request 设置视频id
			request.setVideoIds(id);
			// 调用初始化对象的方法实现删除
			client.getAcsResponse(request);
			return R.ok();
		} catch (ClientException e) {
			e.printStackTrace();
			throw new GuliException(20001, "删除视频失败");
		}
	}
	
	/**
	 * 删除多个阿里云视频的方法
	 * */
	@DeleteMapping("delete-batch")
	public R deleteBatch(@RequestParam("videoIdList")  List<String> videoIdList) {
		vodService.removeMoreAlyVideo(videoIdList);
		return R.ok();
	}
	
	/**
	 * 根据视频id获取视频凭证
	 * */
	@GetMapping("getPlayAuth/{id}")
	public R getPlayAuth(@PathVariable String id) {
		// 创建初始化对象
		DefaultAcsClient client =
				InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
		// 创建获取视频凭证的requset & response
		GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
		// 向requset 设置id
		request.setVideoId(id);
		// 调用初始化对象的方法获取凭证
		GetVideoPlayAuthResponse response = null;
		try {
			response = client.getAcsResponse(request);
		} catch (ClientException e) {
			throw new GuliException(20001, "获取视频凭证失败");
		}
		// 获取凭证
		return R.ok().data("playAuth", response.getPlayAuth());
	}
	
	
}








